﻿using duia.Canteenpay.Domain;
using duia.Canteenpay.Domain.DomainObject;
using duia.Canteenpay.Repository.EntityConfiguration;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace duia.Canteenpay.Repository
{
    /// <summary>
    /// 工作单元实现
    /// </summary>
    public class AppUnitOfWork : DbContext, IAppUnitOfWork
    {

        public AppUnitOfWork(DbContextOptions options) : base(options)
        {

        }
        public DbSet<Users> User { get; set; }
        public DbSet<Dishes> Dishes { get; set; }
        public DbSet<DishesCategory> DishesCategory { get; set; }


        public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                var saveChangesAsync = await base.SaveChangesAsync(cancellationToken);
                return saveChangesAsync;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new UsersEntityTypeConfiguration());
            modelBuilder.ApplyConfiguration(new DishesCategoryEntityTypeConfiguration());
            modelBuilder.ApplyConfiguration(new DishesEntityTypeConfiguration());

            base.OnModelCreating(modelBuilder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //optionsBuilder.UseMySql("Server=49.234.222.247;port=3306;Database=canteen_pay;User Id=root;Password=@Qq101414;Allow User Variables=True;sslMode=None;",
            //    new MySqlServerVersion(new Version(8, 0, 18)), mySqlOptions => mySqlOptions
            //        .CharSetBehavior(CharSetBehavior.NeverAppend));
            base.OnConfiguring(optionsBuilder);
        }
    }
}
